<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>安装 wordpress（nginx）</title>

    <link rel="stylesheet" href="../theme/css/style.css">
    <link rel="stylesheet" href="../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>安装 wordpress（nginx）</h1>    <p>
        under
            <a href="../tags/nginx/">Nginx</a>
            <a href="../tags/wordpress/">WordPress</a>
    </p>
    <p>
        in <a href="../categories/uncategorized/">Uncategorized</a>
    </p>
    <p>Published: 2019-03-25</p>


    <!-- wp:heading -->

<h2>安装步骤</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>操作系统是 Ubuntu 18.04.2 LTS (Bionic Beaver)，amd64。</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->

<h3>安装和配置MySQL</h3>
<!-- /wp:heading -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">apt-get install mysql-server  # 5.7.25-0ubuntu0.18.04.2</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>虽然 wordpress 安装文档建议尽量不要用 root 账户来管理 MySQL，但因为对
MySQL 不熟悉 ，只能用 root 账户运行下面的命令：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">mysql -u root -p</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>建立供 wordpress 使用的 MySQL 数据库和用户账户（替换 &lt;&gt;
中的内容）：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="n">CREATE</span> <span class="n">DATABASE</span> <span class="o">&lt;</span><span class="n">DB_NAME</span><span class="o">&gt;</span><span class="p">;</span>

<span class="n">CREATE</span> <span class="n">USER</span> <span class="s">&quot;&lt;DB_USER&gt;&quot;@&quot;localhost&quot;</span> <span class="n">IDENTIFIED</span> <span class="n">BY</span> <span class="s">&quot;&lt;PASSWORD&gt;&quot;</span><span class="p">;</span>

<span class="n">GRANT</span> <span class="n">ALL</span> <span class="n">PRIVILEGES</span> <span class="n">ON</span> <span class="o">&lt;</span><span class="n">DB_NAME</span><span class="o">&gt;</span><span class="p">.</span><span class="o">*</span> <span class="n">TO</span> <span class="s">&quot;&lt;DB_USER&gt;&quot;@&quot;localhost&quot;</span> <span class="n">IDENTIFIED</span> <span class="n">BY</span> <span class="s">&quot;&lt;DB_PASSWORD&gt;&quot;</span><span class="p">;</span>

<span class="n">FLUSH</span> <span class="n">PRIVILEGES</span><span class="p">;</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:heading {"level":3} -->

<h3>准备 wordpress 目录</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>下载最新的
<a href="https://wordpress.org/download/">wordpress</a>，我下载的版本是5.1.1。</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>将 wordpress 解压到 /path/to/wordpress，确保最顶级的 index.php
文件位于该目录下。</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->

<h3>配置 wordpress</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>进入 /path/to/wordpress 目录，将 wp-config-sample.php 重命名为
wp-config.php，将 DB_NAME、DB_USER 和 DB_PASSWORD 修改为 MySQL
数据库中设置的值。</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>然后访问
https://api.wordpress.org/secret-key/1.1/salt，用这个网页生成的随机代码替换配置文件中的相应内容：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">define( &#39;AUTH_KEY&#39;,         &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;SECURE_AUTH_KEY&#39;,  &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;LOGGED_IN_KEY&#39;,    &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;NONCE_KEY&#39;,        &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;AUTH_SALT&#39;,        &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;SECURE_AUTH_SALT&#39;, &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;LOGGED_IN_SALT&#39;,   &#39;put your unique phrase here&#39; );</span>
<span class="err">define( &#39;NONCE_SALT&#39;,       &#39;put your unique phrase here&#39; );</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:heading {"level":3} -->

<h3>安装和配置 nginx</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>安装 nginx</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">apt-get install nginx  # 1.14.0-0ubuntu1.2</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>在 /etc/nginx/sites-enabled/default 文件中，把 index.php 添加到 index
命令后的一串列表中，并启用将 php 脚本传给 FastCGI
服务器的那一段代码，修改后的代码如下所示：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code>        <span class="err">#</span> <span class="nt">Add</span> <span class="nt">index</span><span class="p">.</span><span class="nc">php</span> <span class="nt">to</span> <span class="nt">the</span> <span class="nt">list</span> <span class="nt">if</span> <span class="nt">you</span> <span class="nt">are</span> <span class="nt">using</span> <span class="nt">PHP</span>
        <span class="p">#</span><span class="nn">index</span> <span class="nt">index</span><span class="p">.</span><span class="nc">html</span> <span class="nt">index</span><span class="p">.</span><span class="nc">htm</span> <span class="nt">index</span><span class="p">.</span><span class="nc">nginx-debian</span><span class="p">.</span><span class="nc">html</span><span class="o">;</span>
        <span class="nt">index</span> <span class="nt">index</span><span class="p">.</span><span class="nc">php</span> <span class="nt">index</span><span class="p">.</span><span class="nc">html</span> <span class="nt">index</span><span class="p">.</span><span class="nc">htm</span> <span class="nt">index</span><span class="p">.</span><span class="nc">nginx-debian</span><span class="p">.</span><span class="nc">html</span><span class="o">;</span>

        <span class="err">#</span> <span class="nt">pass</span> <span class="nt">PHP</span> <span class="nt">scripts</span> <span class="nt">to</span> <span class="nt">FastCGI</span> <span class="nt">server</span>
        <span class="err">#</span>
        <span class="nt">location</span> <span class="o">~</span> <span class="err">\</span><span class="p">.</span><span class="nc">php</span><span class="o">$</span> <span class="p">{</span>
                <span class="err">include</span> <span class="err">snippets/fastcgi-php.conf</span><span class="p">;</span>

                <span class="err">#</span> <span class="err">With</span> <span class="err">php-fpm</span> <span class="err">(or</span> <span class="err">other</span> <span class="err">unix</span> <span class="err">sockets):</span>
                <span class="err">#fastcgi_pass</span> <span class="n">unix</span><span class="p">:</span><span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">run</span><span class="o">/</span><span class="n">php</span><span class="o">/</span><span class="n">php7</span><span class="mf">.0</span><span class="o">-</span><span class="n">fpm</span><span class="o">.</span><span class="n">sock</span><span class="p">;</span>
                <span class="err">fastcgi_pass</span> <span class="n">unix</span><span class="p">:</span><span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">run</span><span class="o">/</span><span class="n">php</span><span class="o">/</span><span class="n">php7</span><span class="mf">.2</span><span class="o">-</span><span class="n">fpm</span><span class="o">.</span><span class="n">sock</span><span class="p">;</span>
        <span class="p">}</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>当然，网站的根目录要设置为 wordpress 所在的目录：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">root /path/to/wordpress;</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>可以看到这里用了 php-fpm 来处理 FastCGI 和 php 之间的通信，安装
php-fpm：</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<div class="highlight"><pre><span></span><code><span class="err">apt-get install php-fpm  # 1:7.2+60ubuntu1</span>
</code></pre></div>


<!-- /wp:code -->

<!-- wp:paragraph -->

<p>不同版本的 php-fpm 的 socket 文件路径不一样，注意确保 fastcgi_pass
后面的 unix socket 文件存在。</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>原理</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>到此，可以通过 http://127.0.0.1/ 来访问搭建的 wordpress
网站了。折腾了一通，目的是实现下面的过程：nginx
收到浏览器发送的请求，找到对应目录下的 wordpress 入口 index.php，通过
fastcgi_pass 交给 php-fpm 执行，wordpress
运行时，所有的持久数据都保存到 MySQL 中，直到把执行结果返回给浏览器。</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>其它</h2>
<!-- /wp:heading -->

<!-- wp:list -->

<ul>
<li>参考资料：<a href="https://codex.wordpress.org/Installing_WordPress">wordpress
    安装文档</a></li>
</ul>
<!-- /wp:list -->
    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../categories/">categories</a>
        <a href="..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>